<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28
 * Time: 17:30
 */

namespace addons\roll\controller;
use app\common\controller\Addon;
use addons\roll\model\RollprizeModel;
use think\Controller;
use think\facade\Request;
use think\Db;
class Index extends Addon
{
    private $rollModel;
    public function __construct()
    {
        parent::__construct();
        $this->rollModel = new RollprizeModel();
    }

    public function index(){
        $get = Request::get();
        if(!isset($get['roll_id'])){
            echo '未传入roll_id';die;
        }
        //查询奖品列表和对应中奖几率
        $prize = Db::table('rh_roll_set_prize')->where(['roll_id'=>$get['roll_id']])->select();
        $prize_name = array_column($prize, 'prize_name');
        $odds = array_column($prize, 'odds');
        
        //查询中奖人员
        $list = Db::table('rh_roll_prize')->where(['roll_id'=>$get['roll_id']])->select();
        $total = Db::table('rh_roll_prize')->where(['roll_id'=>$get['roll_id']])->count();

        //dump($list);die;
        $this->assign('odds', json_encode($odds,JSON_UNESCAPED_UNICODE));
        $this->assign('prize_name', json_encode($prize_name,JSON_UNESCAPED_UNICODE));
        $this->assign('list', $list);
        $this->assign('total', $total);
        return $this->fetch();
    }

    public function rollList(){
        $list = Db::table('rh_roll_list')->select();
        $this->assign('list', $list);
        return $this->fetch('rollList');
    }

    public function addRoll(){
        if(Request::method() == 'POST'){
            $post = Request::post();
            if(Db::table('rh_roll_list')->insert($post)){
                $this->rollList();
            }
        }else{
            return $this->fetch();
        }
    }

    public function deleteRoll(){
        $get = Request::get();
        if(Db::table('rh_roll_list')->where(['id'=>$get['id']])->delete()){
            $this->rollList();
        }
    }

}